package com.xhwl.logistics.strategy.impl;

import com.xhwl.logistics.bo.cent.DeviceBO;
import com.xhwl.logistics.bo.cent.VehicleIntegratedConfigBO;
import com.xhwl.logistics.bo.cent.VehicleSystemInfoBO;
import com.xhwl.logistics.converter.mapstruct.CentDeviceConverter;
import com.xhwl.logistics.dto.http.DeviceInfoCoSonDTO;
import com.xhwl.logistics.dto.http.query.QueryCommonCoSonDTO;
import com.xhwl.logistics.service.business.VehicleAlarmBusinessService;
import com.xhwl.logistics.service.business.VehicleDeviceDataPushBusinessService;
import com.xhwl.logistics.service.cent.QueryDeviceSecondService;
import com.xhwl.logistics.service.impl.http.CoSonHttpServiceImpl;
import com.xhwl.logistics.strategy.IVehicleParkingLotDeviceHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.util.Optionals;

import java.util.*;
import java.util.stream.Collectors;
@Slf4j
//@Service
public class VehicleParkingLotCoSonDeviceHandlerImpl implements IVehicleParkingLotDeviceHandler {

    @Autowired
    private CoSonHttpServiceImpl coSonHttpService;

    @Autowired
    private QueryDeviceSecondService queryDeviceSecondService;

    @Autowired
    private CentDeviceConverter centDeviceConverter;

    @Autowired
    private VehicleAlarmBusinessService vehicleAlarmBusinessService;
    @Autowired
    private VehicleDeviceDataPushBusinessService vehicleDeviceDataPushBusinessService;

    @Override
    public void syncDeviceWithStatus(VehicleIntegratedConfigBO vehicleIntegratedConfigBO) {
        log.info("CoSon sync deviceStatus start!");
        log.info("vehicleIntegratedConfigBO: " + vehicleIntegratedConfigBO);

        VehicleSystemInfoBO vehicleSystemInfoBO = vehicleIntegratedConfigBO.getVehicleSystemInfoBO();
        Integer projectId = vehicleSystemInfoBO.getProjectId();
        Integer productId = vehicleSystemInfoBO.getProductId();
        Integer productCategoryId = vehicleSystemInfoBO.getProductCategoryId();

        List<DeviceInfoCoSonDTO> deviceInfoCoSonDTOList = coSonHttpService.queryDeviceInfo(vehicleIntegratedConfigBO, new QueryCommonCoSonDTO());
//        deviceInfoCoSonDTOList.forEach(deviceInfoCoSonDTO -> {
//            vehicleAlarmBusinessService.sendAlarmForCoSonDeviceStatus(projectId, Integer.valueOf(deviceInfoCoSonDTO.getVehStatus()));
//        });
        List<DeviceBO> deviceListFromCentDevice = queryDeviceSecondService.queryDevicesByProjectId(projectId);
        Map<String, Integer> temp = deviceListFromCentDevice.stream().collect(Collectors.toMap(DeviceBO::getNumber, DeviceBO::getId));
        List<DeviceBO> add = new ArrayList<>();
        List<DeviceBO> update = new ArrayList<>();
        deviceInfoCoSonDTOList.forEach(deviceInfoCoSonDTO -> {
            Optionals.ifPresentOrElse(
                    Optional.ofNullable(temp.get(deviceInfoCoSonDTO.getVehNumber())),
                    id -> {
                        DeviceBO deviceBO = centDeviceConverter.toDeviceBOByCoSon(id, deviceInfoCoSonDTO, projectId, productId, productCategoryId);
                        if(null != deviceInfoCoSonDTO.getVehStatus()){
                            try {
                                vehicleDeviceDataPushBusinessService.coSonDeviceStatus(deviceInfoCoSonDTO, projectId);
                            } catch (Exception exception) {
                                throw exception;
                            }
                        }
                        update.add(deviceBO);
                    },
                    () -> {
                        DeviceBO deviceBO = centDeviceConverter.toDeviceBOByCoSon(deviceInfoCoSonDTO, projectId, productId, productCategoryId);
                        add.add(deviceBO);
                    });
        });

        if (!add.isEmpty()) {
            queryDeviceSecondService.addDevices(add);
        }

        if (!update.isEmpty()) {
            queryDeviceSecondService.updateDevices(update);
        }

        log.info("CoSon sync deviceStatus end!");
    }
}